<?php

namespace App\Http\Controllers\Wechat;
use App\Model\WxButton;
use App\Server\WechatServer;
use Houdunwang\WeChat\WeChat;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class WxButtonController extends Controller
{
    public function __construct()
    {
    }
    public function index()
    {
        $buttons=WxButton::get();
       return view('wechat.wxbutton.index',compact('buttons'));
    }
    public function create()
    {

        return view('wechat.wxbutton.create');
    }
    public function store(Request $request ,WxButton $wxButton)
    {
        $wxButton->name=$request->name;
        $wxButton->data=$request->data;
        $wxButton->save();
        return redirect()->route('wechat.wxbutton.index')->with('success','按钮添加成功');
    }
    public function show(WxButton $wxButton)
    {
    }
    public function edit(WxButton $wxbutton)
    {
//        dd($wxbutton->toArray());

       return view('wechat.wxbutton.edit',compact('wxbutton'));
    }
    public function update(Request $request, WxButton $wxbutton)
    {
        $wxbutton->name=$request->name;
        $wxbutton->data=$request->data;
        $wxbutton->save();
        return redirect()->route('wechat.wxbutton.index')->with('success','按钮编辑成功');
    }
    public function destroy(WxButton $wxbutton)
    {
       $wxbutton->delete();
       return back();
    }
    public function push(WxButton $wxButton,WechatServer $wechatServer){
        $wechatServer->link();
        $res = WeChat::instance('button')->create(json_decode($wxButton['data'],true));
        if ($res['errcode']==0){
            return back()->with('success','按钮推送成功');
        }else{
            return back()->with('warning',$res['errmsg']);
        }
    }
}
